package calao.client.ui;

import calao.client.Desktop;
import calao.client.i18n.Text;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

abstract public class WizardStep
extends Composite
implements ClickListener
{
	
	final Text text = Desktop.getDesktop().text;
	
	final SpacerPanel panel = new SpacerPanel(SpacerPanel.VERTICAL);
	final Label header = new Label();
	final Form form = new Form(Form.EXECUTE);
	final SpacerPanel buttonPanel = new SpacerPanel(SpacerPanel.HORIZONTAL);
	final boolean last;
	
	Wizard wizard;
	int index = -1;
	Button prevButton, nextButton;
	
	public WizardStep(boolean last) 
	{
		this.last = last;
		initWidget(panel);
		panel.add(header);
		form.setButtonsEnabled(false);
		panel.add(form);
	}
	
	public abstract void execute();
	
	void setIndex(int index) 
	{
		this.index = index;
		header.setText(text.stepHeader(index+1, getTitle()));
		if (index>0) {
			buttonPanel.add(prevButton = new Button(text.previous()));
			prevButton.addClickListener(this);
		}
		buttonPanel.add(nextButton = new Button(last? text.complete() : text.next()));
		nextButton.addClickListener(this);
		panel.add(buttonPanel);
	}
	
	public void onClick(Widget sender) 
	{
		if (sender==prevButton) wizard.popLastStep();
		else if (sender==nextButton) if (form.submit()) execute();
	}
	
	public Form getForm()
	{
		return form;
	}

}
